package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.MessageDAO;
import com.xinghou.platform.model.main.Message;

/**
 * 
 * @author wzk
 *
 */
@Service
public class MessageDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Message getMessageById(Integer Id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,Text,Attach_id,Jump_url,Jump_native_page,Status,Create_time FROM message WHERE Msg_id = ?";
		Message message = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return message;
	}

	@Override
	public int updateMessage(Message message) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"UPDATE message SET Sender_id=?,Receiver_id=?,Msg_type=?,Origin=?,TEXT=?,Attach_id=?,Jump_url=?,Jump_native_page=?,STATUS=?,Create_time=? WHERE Msg_id=?"	
				);
		Object[] params = {message.getSenderId(),message.getReceiverId(),message.getMsgType(),message.getOrigin(),message.getText(),message.getAttachId(),
				message.getJumpUrl(),message.getJumpNativePage(),message.getStatus(),message.getCreateTime(),message.getMsgId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveMessage(Message message) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO message ( Sender_id, Receiver_id, Msg_type, Origin, TEXT, Attach_id, Jump_url, Jump_native_page, STATUS, Create_time) VALUES (?,?,?,?,?,?,?,?,?,?);"
				);
		Object[] params = {message.getSenderId(),message.getReceiverId(),message.getMsgType(),message.getOrigin(),message.getText(),message.getAttachId(),
				message.getJumpUrl(),message.getJumpNativePage(),message.getStatus(),message.getCreateTime()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	@Override
	public List<Message> queryMsg(Integer sendid,Integer receiverid, Integer origin,Integer state,int startNum,int limitNum) throws DataAccessException{
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,Text,Attach_id,Jump_url,Jump_native_page,Status,Create_time FROM message WHERE Receiver_id = ?");
		List<Object> objlist = new ArrayList<Object>();
		objlist.add(receiverid);
		if(origin != -1)
		{
			sql.append(" AND Origin=?");
			objlist.add(origin);
		}
		if(state != -1)
		{
			sql.append(" AND Status=?");
			objlist.add(state);
		}
		if(sendid != null)
		{
			sql.append(" AND Sender_id=?");
			objlist.add(sendid);
		}
		
		objlist.add(startNum);
		objlist.add(limitNum);
		Object[] obj = objlist.toArray();
		sql.append(" AND Msg_id>? ORDER BY Msg_id ASC LIMIT ?");
		
		List<Message> list = jdbcTemplate.query(sql.toString(), obj, new MessageMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@Override
	public List<Message> queryMsgGroupBySendId(Integer receiverid, Integer state, int startNum,int limitNum) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,Text,Attach_id,Jump_url,Jump_native_page,Status,Create_time FROM (");
		sql.append("SELECT Msg_id,Sender_id,Receiver_id,Msg_type,Origin,Text,Attach_id,Jump_url,Jump_native_page,Status,Create_time FROM message WHERE 1=1");
		List<Object> objlist = new ArrayList<Object>();
		if(receiverid != null)
		{
			sql.append(" AND Receiver_id = "+receiverid);
			objlist.add(receiverid);
		}
		if(state != -1)
		{
			sql.append(" AND Status= "+state);
			objlist.add(state);
		}
		sql.append(" ORDER BY Create_time DESC) t GROUP BY t.Sender_id");
		if(startNum==0){
			startNum = Integer.MAX_VALUE;
		}
		//TODO暂且不做分页
		//objlist.add(startNum);
		//objlist.add(limitNum);
		//Object[] obj = objlist.toArray();
		List<Message> list = jdbcTemplate.query(sql.toString(), new MessageMapper());
		
		CustomerContextHolder.clearCustomerType();
		return list;
	}
	
	@Override
	public int queryMsgCount(Integer sendid,Integer receiverid, Short origin, Integer state)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT COUNT(Msg_id) AS count FROM message WHERE Receiver_id = ?");
		List<Object> objlist = new ArrayList<Object>();
		objlist.add(receiverid);
		if(origin != -1)
		{
			sql.append(" AND Origin=?");
			objlist.add(origin);
		}
		if(state != -1)
		{
			sql.append(" AND Status=?");
			objlist.add(state);
		}
		if(sendid != null)
		{
			sql.append(" AND Sender_id=?");
			objlist.add(sendid);
		}
		Object[] obj = objlist.toArray();
		int count = jdbcTemplate.queryForObject(sql.toString(), obj, new MessageCountMapper());
		CustomerContextHolder.clearCustomerType();
		return count;
	}
	
	private static final class MessageMapper implements RowMapper<Message> {
		public Message mapRow(ResultSet rs, int rowNum) throws SQLException {
			Message message = new Message();
			message.setMsgId(rs.getInt("Msg_id"));
			message.setSenderId(rs.getInt("Sender_id"));
			message.setReceiverId(rs.getInt("Receiver_id"));
			message.setMsgType(rs.getShort("Msg_type"));
			message.setOrigin(rs.getShort("Origin"));
			message.setText(rs.getString("Text"));
			message.setAttachId(rs.getInt("Attach_id"));
			message.setJumpUrl(rs.getString("Jump_url"));
			message.setJumpNativePage(rs.getString("Jump_native_page"));
			message.setStatus(rs.getShort("Status"));
			//message.setCreateTime(rs.getTimestamp("Create_time"));
			return message;
		}
	}

	private static final class MessageCountMapper implements RowMapper<Integer> {
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			Integer count = rs.getInt("count");
			return count;
		}
	}*/
}
